package cn.edu.tjufe.llp.tin.controller.part03;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.tjufe.llp.tin.dto.part03.AddressDto;
import cn.edu.tjufe.llp.tin.dto.part03.ContactDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "contact", description = "the Contact API")
@RequestMapping("/part03")
@RestController
public class ContactController {

	@Operation(summary = "通过姓名查找联系人", description = "Name search by %name% format", tags = { "contact" })
	@GetMapping(value = "/contacts")
	public List<ContactDto> findAll(
			@Parameter(description = "Page number, default is 1") @RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@Parameter(description = "Name of the contact for search.") @RequestParam(required = false) String name) {
		List<ContactDto> result = new ArrayList<>();

		ContactDto dto = new ContactDto();
		dto.setName("peter");
		dto.setPhone("13912345678");
		dto.setEmail("peter@domain.com");
		dto.setPostalCode("300000");
		result.add(dto);

		dto = new ContactDto();
		dto.setName("赵钱孙");
		dto.setPhone("13098765432");
		dto.setEmail("zhaoqiansun@company.com");
		dto.setPostalCode("100000");
		dto.setAddress1("天津市");
		dto.setAddress1("河西区");
		dto.setAddress1("珠江道25号");
		dto.setDob(LocalDate.now().minusYears(20));
		dto.setNote("备注");
		result.add(dto);

		return result;
	}

	@Operation(summary = "通过ID查找联系人", description = "Returns a single contact", tags = { "contact" })
//	@ApiResponses(value = {
//			@ApiResponse(responseCode = "200", description = "successful operation", 
//					content = @Content(schema = @Schema(implementation = ContactDto.class))),
//			@ApiResponse(responseCode = "404", description = "Contact not found") })
	@GetMapping(value = "/contacts/{contactId}", produces = { "application/json", "application/xml" })
	public ContactDto findContactById(
			@Parameter(description = "Id of the contact to be obtained. Cannot be empty.", required = true) @PathVariable long contactId) {
		ContactDto result = new ContactDto();
		result.setName("peter");
		result.setPhone("13912345678");
		result.setEmail("peter@domain.com");
		result.setPostalCode("300000");

		return result;
	}
	
	@Operation(summary = "添加一个新的联系人", description = "", tags = { "contact" })
	@PostMapping(value = "/contacts")
	public ContactDto addContact(
			@Parameter(description = "Contact to add. Cannot null or empty.", required = true, schema = @Schema(implementation = ContactDto.class)) @Valid @RequestBody ContactDto contact) {

		ContactDto result = new ContactDto();
		result.setId(1);
		result.setName(contact.getName());
		result.setPhone(contact.getPhone());
		result.setEmail(contact.getEmail());
		result.setAddress1(contact.getAddress1());
		result.setAddress2(contact.getAddress2());
		result.setAddress3(contact.getAddress3());
		result.setPostalCode(contact.getPostalCode());
		result.setDob(contact.getDob());
		result.setNote(contact.getNote());
		
		return result;
	}
	

	@Operation(summary = "修改一个已经存在的联系人", description = "", tags = { "contact" })
	@PutMapping(value = "/contacts/{contactId}", consumes = { "application/json", "application/xml" })
	public ResponseEntity<Void> updateContact(
			@Parameter(description = "Id of the contact to be update. Cannot be empty.", required = true) @PathVariable long contactId,
			@Parameter(description = "Contact to update. Cannot null or empty.", required = true, schema = @Schema(implementation = ContactDto.class)) @Valid @RequestBody ContactDto contact) {
		// return ResponseEntity.noContent().build();
		return ResponseEntity.ok().build();
	}

	@Operation(summary = "修改一个已经存在的联系人", description = "", tags = { "contact" })
	@PutMapping(value = "/contacts")
	public ResponseEntity<Void> updateContact2(
			@Parameter(description = "Contact to update. Cannot null or empty.", required = true, schema = @Schema(implementation = ContactDto.class)) @Valid @RequestBody ContactDto contact) {
		return ResponseEntity.ok().build();
	}
	
	@Operation(summary = "修改已经存在的联系人的地址", description = "", tags = { "contact" })
	@PatchMapping("/contacts/{contactId}")
	public ResponseEntity<Void> updateAddress(
			@Parameter(description = "Id of the contact to be update. Cannot be empty.", required = true) @PathVariable long contactId,
			@Parameter(description = "Contact's address to update.", required = true, schema = @Schema(implementation = AddressDto.class)) @RequestBody AddressDto address) {
		// return ResponseEntity.noContent().build();
		return ResponseEntity.ok().build();
	}
	
	@Operation(summary = "删除一个联系人", description = "", tags = { "contact" })
	@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "successful operation"),
			@ApiResponse(responseCode = "404", description = "Contact not found") })
	@DeleteMapping(path = "/contacts/{contactId}")
	public ResponseEntity<Void> deleteContactById(
			@Parameter(description = "Id of the contact to be delete. Cannot be empty.", required = true) @PathVariable long contactId) {
		// return ResponseEntity.noContent().build();
		return ResponseEntity.ok().build();
	}
}
